package com.itheima.reggie.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 加入IOC容器
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    //依赖注入
    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    /**
     * 用户登录
     *
     * @param employee
     * @return
     */
    @Override
    public R<Employee> login(Employee employee) {
        //1.根据用户名查询用户
        //1.1调用到层方法获取用户信息
        Employee loginEmployee = employeeMapper.login(employee);
        //1.2判断用户是否存在
        if (loginEmployee == null) {
            //提示用户名或密码异常
            throw new BusinessException("用户名或密码错误");
        }
        //2.用户名没有问题判断密码
        //2.1.拿到用户提交的明文密码加密成MD5
        String MD5 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //2.2判断密码是否正确
        if (!loginEmployee.getPassword().equals(MD5)) {
            //提示密码异常
            throw new BusinessException("密码错误");
        }
        //3.判断用户是否为禁用
        if (loginEmployee.getStatus() == 0) {
            throw new BusinessException("您已被禁用,请联系管理员");
        }
        return R.success(loginEmployee);
    }

    /**
     * 根据id查询用户数据
     *
     * @param id
     * @return
     */
    @Override
    public R<Employee> selectById(Long id) {
        return R.success(employeeMapper.selectById(id));
    }

    /**
     * 添加用户
     *
     * @param employee
     * @return
     */
    @Override
    public boolean save(Employee employee) {
        //1.调用dao层登录方法,校验用户唯一性
        Employee loginEmployee = employeeMapper.login(employee);
        if (loginEmployee != null) {
            throw new BusinessException("用户已存在,请重新输入");
        }
        //2.用户名唯一进行添加用户信息
        //2.1.进行用户密码加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //2.2.状态设置为1代表启用
        employee.setStatus(1);
        //2.3.设置当前时间为创建对象的时间
        employee.setCreateTime(LocalDateTime.now());
        //2.4.设置修改时间
        employee.setUpdateTime(LocalDateTime.now());
        //3.调用添加方法并返回布尔类型值 > 0 表示添加成功
        return employeeMapper.save(employee) > 0;
    }

    /**
     * 分页查询员工数据
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        //1.设置分页查询第几页和每页多少条
        PageHelper.startPage(page, pageSize);
        //2.执行查询所有数据的sql语句方法
        List<Employee> employeeList = employeeMapper.findByName(name);
        //3.使用PageInfo封装上面的结果
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        Page<Employee> objectPage = new Page<>();
        objectPage.setRecords(pageInfo.getList());
        objectPage.setTotal(pageInfo.getTotal());
        return objectPage;
    }

    /**
     * 修改员工数据
     *
     * @param employee
     * @return
     */
    @Override
    public boolean update(Employee employee) {
        //根据id获取登录明是否重复
        Employee employeeObj = employeeMapper.selectById(employee.getId());
        if (!(employeeObj.getUsername().equals(employee.getUsername()) && employeeObj.getId().equals(employee.getId()))) {
            throw new BusinessException("用户已存在,请重新输入");
        }
        employee.setUpdateTime(LocalDateTime.now());
        // > 0 表示修改成功
        return employeeMapper.update(employee) > 0;
    }
}
